package com.oracle.oa.dao.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.oracle.oa.biz.UserInfoBiz;
import com.oracle.oa.dao.ManualSignDao;
import com.oracle.oa.dao.impl.BaseDaoImpl;
import com.oracle.oa.entity.Manualsign;
import com.oracle.oa.entity.Userinfo;

public class ManualSignDaoImpl extends BaseDaoImpl implements ManualSignDao {

	/**
	 * 通过时间段获取签卡信息
	 */
	
	public List<Manualsign> getByTime(String begintime, String endtime) {
		// 通过时间获取签卡信息表
		Session session = this.getHibernateTemplate().getSessionFactory()
				.openSession();
		List<Manualsign> list = null;
		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		list = c.list();
		session.close();
		return list;
	}

	/**
	 * 通过用户id获取某用户的所有签卡信息
	 */
	
	public List<Manualsign> getByUserid(int userid) {
		// ͨ通过用户id获取签卡信息
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		List<Manualsign> list = null;
		String hql = "from Manualsign m where userid=" + userid;
		Query query = session.createQuery(hql);
		list = query.list();
		session.close();
		return list;
	}

	/**
	 * 通过部门名称获取该部门所有的签卡信息
	 */
	
	public List<Manualsign> getByDept(String departname) {
		// 通过部门名称获取签卡信息
		List<Manualsign> list = null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();

		// 调用底层根据属性获取签卡信息
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserInfoBiz msb = (UserInfoBiz) ac.getBean("ubiz");
		List<Userinfo> ulist = msb.getByProperty(Userinfo.class, "departid",
				departname);

		Criteria c = session.createCriteria(Manualsign.class);

		c.add(Restrictions.in("userinfo", ulist));
		list = c.list();
		session.close();
		return list;
	}

	/**
	 * 查询某一时间段内某部门的所有签卡信息
	 */
	
	public List<Manualsign> getByTimeAndDept(String begintime, String endtime,
			String departname) {
		// 查询某一时间段某部门的签卡信息
		List<Manualsign> list = null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();

		// 调用底层根据属性获取签卡信息
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserInfoBiz msb = (UserInfoBiz) ac.getBean("ubiz");
		List<Userinfo> ulist = msb.getByProperty(Userinfo.class, "departid",
				departname);

		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		c.add(Restrictions.in("userinfo", ulist));

		list = c.list();
		session.close();
		return list;
	}

	/**
	 * 根据id查询某一时间段内该用户的签卡信息
	 */
	
	public List<Manualsign> getByIdAndTime(String begintime, String endtime,
			int userid) {
		// 查询某时间段内某用户id的签卡信息
		List<Manualsign> list = null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		c.add(Restrictions.eq("userinfo.userid", userid));
		list = c.list();
		session.close();
		return list;
	}

	/**
	 * 根据用户名查询某一时间段内该用户的签卡信息
	 */
	
	public List<Manualsign> getByUnameAndTime(String begintime, String endtime,
			String username) {
		// 查询某时间段内某用户名的签卡信息
		List<Manualsign> list = null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();

		// 调用底层根据属性获取签卡信息
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserInfoBiz msb = (UserInfoBiz) ac.getBean("ubiz");
		List<Userinfo> ulist = msb.getByProperty(Userinfo.class, "username",
				username);

		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		c.add(Restrictions.in("userinfo", ulist));
		list = c.list();
		
		return list;
	}

	/**
	 * 获取一个时间段内某用户的迟到天数
	 */
	
	public List<Manualsign> getLate(Userinfo user, String begintime,
			String endtime) {
		// 获取某时间段内某用户迟到天数
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		List<Manualsign> list = new ArrayList<Manualsign>();
		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.eq("userinfo", user));
		c.add(Restrictions.between("signtime", begintime, endtime));
		SimpleDateFormat sf = new SimpleDateFormat("hh:mm");

		c.add(Restrictions.eq("signtag", "签到"));
		List<Manualsign> list1 = null;
		list1 = c.list();

		List<String> timelist = new ArrayList<String>();
		for (int i = 0; i < list1.size(); i++) {
			timelist.add(list1.get(i).getSigntime());
			try {
				if (sf.parse(timelist.get(i).substring(11, 16)).getTime() > sf
						.parse("08:30").getTime()) {
					list.add(list1.get(i));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		session.close();
		return list;
	}

	/**
	 * 获取一个时间段内某用户的早退天数
	 */
	
	public List<Manualsign> getEarly(Userinfo user, String begintime,
			String endtime) {
		// 获取某时间段内某用户早退天数
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		List<Manualsign> list = new ArrayList<Manualsign>();
		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.eq("userinfo", user));
		c.add(Restrictions.between("signtime", begintime, endtime));
		SimpleDateFormat sf = new SimpleDateFormat("hh:mm");

		c.add(Restrictions.eq("signtag", "签退"));
		List<Manualsign> list1 = null;
		list1 = c.list();

		List<String> timelist = new ArrayList<String>();
		for (int i = 0; i < list1.size(); i++) {
			timelist.add(list1.get(i).getSigntime());
			try {
				if (sf.parse(timelist.get(i).substring(11, 16)).getTime() < sf
						.parse("17:30").getTime()) {
					list.add(list1.get(i));
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		session.close();
		return list;
	}

	/**
	 * 查询一个时间段内某用户的出勤率
	 */
	
	public double getChuqin(Userinfo user, String begintime, String endtime, String str) {
		// 计算出勤率
		double lv=0;
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		double allday=0;
		try {
			allday=(sf.parse(endtime).getTime()-sf.parse(begintime).getTime())/1000/60/60/24;
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		double day=this.getByIdAndTime(begintime, endtime,user.getUserid()).size()/2;
		if("day".equals(str)){
			lv=day/1;
		}else if("week".equals(str)){
			lv=day/(allday-2);
		}else if("mouth".equals(str)){
			lv=day/(allday-8);
		}else if("year".equals(str)){
			lv=day/(allday-96);
		}else{
			lv=day/allday;
		}
		
		return lv;
	}

	/**
	 * 普通用户查询考勤（只能查询自己本人的考勤记录）
	 */
	
	public List<Manualsign> getPuByTime(Userinfo user, String begintime,
			String endtime) {
		// 普通用户查询（只能查询自己的考勤记录）
		List<Manualsign> list = null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();

		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		c.add(Restrictions.eq("userinfo", user));

		list = c.list();
		session.close();
		return list;
	}

	/**
	 * 部门管理员查询考勤记录（查询本部门所有员工和自己）
	 */
	
	public List<Manualsign> getPgByTime(Userinfo user, String begintime,
			String endtime) {
		// 部门管理员查询考勤记录（查询本部门所有员工和自己）
		List<Manualsign> list=null;
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		
		int deptname = user.getDepartid().getDepartid();
		// 调用底层根据属性获取该部门所有员工
		 //与xml文件关联
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		UserInfoBiz ub = (UserInfoBiz) ac.getBean("ubiz");
		List<Userinfo> ulist = ub.getByProperty(Userinfo.class, "departid",
				deptname);
		
		Criteria c = session.createCriteria(Manualsign.class);
		c.add(Restrictions.between("signtime", begintime, endtime));
		c.add(Restrictions.in("userinfo", ulist));
		
		list = c.list();
		session.close();
		return list;
	}


}
